package cn.edu.zut.dao;

import cn.edu.zut.domin.Book;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


public interface IBookDao {
    /**
     * 查询所有书
     * @return
     * @throws Exception
     */
    @Select("select * from book")
    public List<Book> findAll() throws Exception;

    /**
     * 查询一本书的ById
     * @return
     * @throws Exception
     */
    @Select("select * from book where bookId = #{bookId}")
    public List<Book> findBookById(String id) throws Exception;
    /**
     * 通过姓名从查询名称
     * @param name
     * @return
     * @throws Exception
     */
    @Select("select * from book where name = #{name}")
    public List<Book> findBookByName(String name) throws Exception;
    /**
     * 保存一本书
     * @param book
     * @throws Exception
     */
    @Insert("insert into book(bookId,typeId,name,publisher,publishDate,price,summary,AllAmount,lentAmount,registerDate)" +
            " values(#{bookId},#{typeId},#{name},#{publisher},#{publishDate},#{price},#{summary},#{AllAmount},#{lentAmount},#{registerDate})")
    public void saveBook(Book book) throws Exception;

    /**
     * 删除一本书
     * @param id
     * @throws Exception
     */
    @Delete("delete from book where bookId=#{bookId}")
    public void deleteBook(String id) throws Exception;

    /**
     * 更新一本书byid
     * @param book
     * @throws Exception
     */
    @Update("update book set typeId=#{typeId},name=#{name},publisher=#{publisher},publishDate=#{publishDate}," +
            "price=#{price},summary=#{summary},AllAmount=#{AllAmount},lentAmount=#{lentAmount},registerDate=#{registerDate} " +
            "where bookId=#{bookId}")
    public void updateBook(Book book) throws Exception;

    /**
     *书名模糊
     * @param name
     * @return
     * @throws Exception
     */
    @Select("select * from book where name like  CONCAT('%',#{name},'%')")
    public List<Book> findBookByNameMOHU(String name) throws Exception;

    /**
     * 出版社查询
     * @param name
     * @return
     * @throws Exception
     */
    @Select("select * from book where publisher = #{publisher}")
    public List<Book> findBookByPublisher(String name) throws  Exception;

    /**
     * 查询所有明细信息和借阅信息
     * @param bookId
     * @return
     */
    @Select("select * from Book where bookId=#{bookId}")
    @Results({
            @Result(column="bookId",property="bookId"),
            @Result(property = "bookLendInfos", javaType = List.class, column = "bookId",
                    many = @Many(select = "cn.edu.zut.dao.IBookLendInfoDao.getBookLendInfoByBookId",fetchType = FetchType.EAGER) )    })
    public List<Book> selectBookLendInfoByBookId(String bookId);

    /**
     * 根据图书编号来查询该书的明细信息和借阅信息
     * @return
     */
    @Select("select * from Book")
    @Results({
            @Result(column="bookId",property="bookId"),
            @Result(property = "bookLendInfos", javaType = List.class, column = "bookId",
                    many = @Many(select = "cn.edu.zut.dao.IBookLendInfoDao.getBookLendInfoByBookId",fetchType = FetchType.EAGER) )    })
    public List<Book> selectAllBookLendInfoByBookId();

}
